package data

import (
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

type CargoOwnerDto struct {
	Id            int    `orm:"pk;column(id)"`
	IsCorp        bool   `orm:"column(is_corp)"`
	CorpName      string `orm:"column(corp_name)"`
	CorpAddr      string `orm:"column(corp_addr)"`
	CorpTelephone string `orm:"column(telephone)"`
}
type CargoOwnerGetDto struct {
	Name          string `orm:"column(name)"`
	IsCorp        bool   `orm:"column(is_corp)"`
	Phone         string `orm:"column(phone)"`
	CorpName      string `orm:"column(corp_name)"`
	CorpAddr      string `orm:"column(corp_addr)"`
	Telpehone     string `orm:"column(telephone)"`
	BizLicenseUrl string `orm:"column(biz_license)"`
}

func init() {
	orm.RegisterModel(new(CargoOwnerDto))
}

func (c *CargoOwnerDto) TableName() string {
	return "m_cargo_owner"
}
func (r MemberRepo) UpdateCargoOwner(dto *CargoOwnerDto) (err error) {
	var mbrType string
	mbr := &MemberDto{Id: dto.Id, TypeId: 1, AuthState: 1}
	o := orm.NewOrm()
	if _, err = o.Update(dto); err == nil {
		if mbrType, err = r.GetMemberType(dto.Id); err == nil {
			o.Update(mbr, "AuthState")
			if mbrType == "0" {
				_, err = o.Update(mbr, "TypeId")
				return err
			}
		}
	}

	return err
}

func (r MemberRepo) GetCargoOwner(id int) (dto CargoOwnerGetDto, err error) {
	o := orm.NewOrm()
	dto = CargoOwnerGetDto{}
	err = o.Raw(SQL_Mbr_Get_Cargo_Owner, id).QueryRow(&dto)
	return
}

func (r MemberRepo) EnsureCargoOwner(id int) bool {
	var list orm.ParamsList
	o := orm.NewOrm()
	num, _ := o.Raw(SQL_Mbr_Ensure_Cargo_Owner, id).ValuesFlat(&list)
	return num == 1
}
